package com.ey12.db.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmNews;

/**
 * Data access object (DAO) for domain model class GmNews.
 * 
 * @see com.ey12.db.domain.GmNews
 * @author MyEclipse Persistence Tools
 */

public class GmNewsDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmNewsDAO.class);
	// property constants
	public static final String NEWS_TITLE = "newsTitle";
	public static final String NEWS_CONTENT = "newsContent";
	public static final String PUBLISH_BY = "publishBy";
	public static final String PUBLISH_DATE = "publishDate";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmNews transientInstance) {
		log.debug("saving GmNews instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmNews persistentInstance) {
		log.debug("deleting GmNews instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmNews findById(java.lang.Integer id) {
		log.debug("getting GmNews instance with id: " + id);
		try {
			GmNews instance = (GmNews) getHibernateTemplate().get(
					"com.ey12.db.domain.GmNews", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmNews instance) {
		log.debug("finding GmNews instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmNews instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmNews as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByNewsTitle(Object newsTitle) {
		return findByProperty(NEWS_TITLE, newsTitle);
	}

	public List findByNewsContent(Object newsContent) {
		return findByProperty(NEWS_CONTENT, newsContent);
	}

	public List findByPublishBy(Object publishBy) {
		return findByProperty(PUBLISH_BY, publishBy);
	}

	public List findByPublishDate(Object publishDate) {
		return findByProperty(PUBLISH_DATE, publishDate);
	}

	public List findAll() {
		log.debug("finding all GmNews instances");
		try {
			String queryString = "from GmNews";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findHomeList(){
		try {
			String queryString = "from GmNews order by id desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findNoticeList(){
		try {
			String queryString = "from GmNews order by publishDate desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public GmNews merge(GmNews detachedInstance) {
		log.debug("merging GmNews instance");
		try {
			GmNews result = (GmNews) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmNews instance) {
		log.debug("attaching dirty GmNews instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmNews instance) {
		log.debug("attaching clean GmNews instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmNewsDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmNewsDAO) ctx.getBean("GmNewsDAO");
	}
}